package com.yang.inventorymanagement.controllerview.suppliers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.inventorymanagement.pojo.Suppliers;
import com.yang.inventorymanagement.service.SuppliersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Lazy
@Component("SupplierAddManageSwing")
@Scope("prototype")
public class SupplierAddManageSwing extends JDialog {


    @Autowired
    private SuppliersService suppliersService;


    private JPanel contentPane;
    private JTextField suppliersNameField;
    private JTextField contactNameField;

    private JTextField contactEmailField;


    public SupplierAddManageSwing() {

        setModal(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置窗口大小
        setBounds(100, 100, 573, 404);
        //设置居中显示
        setLocationRelativeTo(null);
        //设置不可编辑
        setResizable(false);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        Border titleBorder = BorderFactory.createTitledBorder("供应商新增");
        contentPane.setBorder(titleBorder);
        setContentPane(contentPane);


        JLabel jLabel = new JLabel("添加供应商");
        jLabel.setBounds(210, 10, 142, 32);
        jLabel.setFont(new Font("宋体", Font.PLAIN, 24));


        JLabel suppliersNameLable = new JLabel("供应商名称：");
        suppliersNameLable.setBounds(136, 118, 80, 15);
        //用户输入框
        suppliersNameField = new JTextField();
        //userNameField.setColumns(10);
        suppliersNameField.setBounds(210, 115, 150, 21);
        contentPane.add(suppliersNameField);

        JLabel contactName = new JLabel("联系人姓名：");
        contactName.setBounds(136, 148, 80, 15);
        contentPane.add(contactName);
        //用户输入框
        contactNameField = new JTextField();
        //userNameField.setColumns(10);
        contactNameField.setBounds(210, 145, 150, 21);
        contentPane.add(contactNameField);

        JLabel emailLable = new JLabel("联系人邮箱：");
        emailLable.setBounds(136, 178, 80, 15);
        contentPane.add(emailLable);

        contactEmailField = new JTextField();
        contactEmailField.setColumns(10);
        contactEmailField.setBounds(210, 175, 150, 21);
        contentPane.add(contactEmailField);

        contentPane.setLayout(null);
        contentPane.add(jLabel);
        contentPane.add(suppliersNameLable);


        JButton btnSubmit = new JButton("新增");
        //新增用户信息
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确定新增？") == 0) {
                    Suppliers suppliers = new Suppliers();
                    //获取userId

                    String suppliername = suppliersNameField.getText();
                    String contactname = contactNameField.getText();
                    String contactemail = contactEmailField.getText();


                    //数据检查

                    if (suppliername.equals("") || contactname.equals("") || contactemail.equals("")) {
                        JOptionPane.showMessageDialog(null, "填写错误，请重新填写", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    /**
                     * 邮箱格式校验
                     */
                    if(!validateEmail(contactemail)){
                        JOptionPane.showMessageDialog(null, "填写错误，注意邮箱格式", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
                     suppliersQueryWrapper.eq("SupplierName", suppliername);

                    Suppliers one = suppliersService.getOne(suppliersQueryWrapper);
                    if (one!=null){
                        JOptionPane.showMessageDialog(null, "填写错误。供应商名已存在", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }


                    //封装数据
                    suppliers.setSuppliername(suppliername);
                    suppliers.setContactname(contactname);
                    suppliers.setContactemail(contactemail);



                    //新增
                    int result = suppliersService.insertSuppliers(suppliers);
                    if (result > 0) {
                        JOptionPane.showMessageDialog(null, "新增成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                        // SessionUtils.getSession().put("user", userService.getById(userId));
                        setVisible(true);
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "新增失败", "提示", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        btnSubmit.setBounds(230, 275, 93, 23);
        contentPane.add(btnSubmit);


    }

    /**
     * 邮箱格式校验
     * @param email
     * @return
     */
    public static boolean validateEmail(String email) {
        Pattern pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
}
